package com.jhmk.jhecis.config;
import cn.hutool.http.HttpRequest;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jhmk.jhecis.dto.RequestHeader;
import com.jhmk.jhecis.interceptor.GlobalResponseInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import cn.hutool.http.HttpUtil;
import java.util.ArrayList;
import java.util.List;

/**register.RestSharpPost
 * Js脚本中Java方法在此类中添加
 */
@Component
public  class JSRegister {

    private static final Logger log = LoggerFactory.getLogger(GlobalResponseInterceptor.class);

    /**
     *日志
     * js中使用【register.log('')】调用
     * @param msg
     */
    public  void myLog(String msg){
        log.info("js脚本打印:"+msg);
    }

    /**
     * 发送RestGet请求
     * @param path 路径
     * @param param 子路径
     * @param headers head
     * @return
     */
    public  String RestSharpGet(String path, String param,String headers) {
        String data = "";
        String lasturl =  path + param;
        List<RequestHeader> headList=null;
        System.out.println(headers);
        if (headers!=null){
            ObjectMapper mapper = new ObjectMapper();
            try {
                JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, RequestHeader.class);
                headList= (List<RequestHeader>) mapper.readValue(headers, javaType);
            }
            catch (Exception e){
                System.out.println("header报错");
            }
        }
        try
        {
            HttpRequest request= HttpUtil.createGet(lasturl);
            request.header("Content-Type", "application/json; charset=utf-8");
            //request.header("auth","jhecis");
            if (headList!=null){
                headList.forEach(item->{ request.header(item.Name, item.Value); });
            }
            data= request.execute().body();

        }catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 发送RestPost请求
     * @param path 路径
     * @param param 子路径
     * @param Data body数据
     * @param headers 请求头
     * @return
     */
    public  String RestSharpPost(String path, String param,String Data,String headers) {
        String data = "";
        String lasturl =  path + param;
        List<RequestHeader> headList=null;
        System.out.println(headers);
        if (StringUtils.isNotBlank(headers)){
            ObjectMapper mapper = new ObjectMapper();
            try {
                JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, RequestHeader.class);
                headList= (List<RequestHeader>) mapper.readValue(headers, javaType);
            }
            catch (Exception e){
                System.out.println("header报错");
            }
        }
        try{
            HttpRequest request= HttpUtil.createPost(lasturl);
            request.header("Content-Type", "application/json; charset=utf-8");
            //request.header("auth","jhecis");
            if (headList!=null){
                headList.forEach(item->{  request.header(item.Name, item.Value); });
            }
            request.body(Data);
            data = request.execute().body();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}
